<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class District extends Admin {

	public function __construct()
    {
        parent::__construct();
		$this->load->model("district_model");
    }
	public function index()
	{
        $page = $this->uri->segment(4);
        $page_size = $this->getPageSize;
        $param = array();
        $param['keyword'] = $this->input->get('keyword');
        $param['parent'] = $this->input->get('parent');
        $param['child'] = $this->input->get('child');
        $total = $this->district_model->count_all($param);
        $records = $this->district_model->find_all($param,$page, $page_size);
        $page_url = base_url('/cp/district/index');
        $pagination = $this->get_search_paging($page_url, $total, $page, $page_size );
        $data = array(
            "total" => $total,
            "records" => $records,
            "pagination" => $pagination,
            'param'=>$param
        );
        $this->layout->view($data);
	}
	public function add(){
		$data = array();
		$data['provinder'] = $this->mprovinder->getListAll();
		$this->load->view('admin/district/add',$data);
	}
	public function edit($id=null){
		$data = array();
		$data['items']= $this->district_model->getSingle($id);

		$data['provinder'] = $this->mprovinder->getListAll();
		$this->load->view('admin/district/edit',$data);
	}

	public function save(){
		$data = array();
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('provinder', 'provinder', 'required');
		$this->form_validation->set_rules('orderNo', 'orderNo', 'required');
		$data['flag'] = true;
		if ($this->form_validation->run() == TRUE){
			$obj['name'] = $this->input->post('name');
			$obj['provinderId'] = $this->input->post('provinder');
			$obj['orderNo'] = $this->input->post('orderNo');

			$objId = $this->district_model->insert($obj);

			if($objId) {
				$data['msg'] = "Them moi thanh cong";
			}else {
				$data['msg'] = "Loi khi them moi";
				$data['flag'] = false;
			}

		}else {
			$data['msg'] = "Thong tin sai";
			$data['flag'] = false;
		}
		echo json_encode($data);

		exit();
	}

	public function do_update(){
		$data = array();
		$data['flag'] 	= true;
		$obj['name'] 	= $this->input->post('name');
		$obj['provinderId'] 	= $this->input->post('provinder');
		$obj['orderNo'] = $this->input->post('orderNo');
		$id 			= $this->input->post('id');
		$objId = $this->district_model->update($obj,$id);
		if($objId){
			$data['msg'] = "Update thanh cong";
		}else {
			$data['msg'] = "Loi khi them moi";
			$data['flag'] = false;
		}
		echo json_encode($data);
		exit();
	}



	public function edit1($id=null){
		$data = array();
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('keyword', 'Keyword', 'required');
		$this->form_validation->set_rules('description', 'Description', 'required');
		if ($this->form_validation->run() == TRUE){
			$data['name'] 			= trim($this->input->post('name'));
			if($this->input->post('level')){
				if($this->input->post('parent')){
					$data['level'] 		= $this->input->post('level');
					//$data['sublevel'] 	= $this->input->post('level');
					$data['parent']		= $this->input->post('parent');
				}else {
					$data['level'] 		= $this->input->post('level');
					//$data['sublevel'] 	= $this->input->post('level');
					$data['parent']		= $this->input->post('level');
				}

			}else {
				$data['level'] 		= 0;
				//$data['sublevel'] 	= 0;
				$data['parent']		= 0;
			}

			$data['is_vip'] 		= $this->input->post('is_vip');
			$data['orderNo'] 		= $this->input->post('orderNo');
			$data['css']			= trim($this->input->post('css'));
			$data['title'] 			= trim($this->input->post('title'));
			$data['type'] 			= trim('category');
			$data['description']	= trim($this->input->post('description'));
			$data['status'] 		= 1;
			$categoryId =	$this->mcategory->update($data,$this->input->post('id'));
			if($categoryId){
				redirect('admin/category');
			}
		}
		if(!empty($id)){
			$data['items']= $this->mcategory->getSingle($id);
			$data['sub_level'] = $this->mcategory->getSubLevel($data['items']['level']);
		}
		$data['level'] = $this->mcategory->getListParent();

		$this->layout->view('admin/category/edit',$data);
	}

}